package windows;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import ships.Destroyer;
import ships.Transport;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import controllers.DataInsertion;
import controllers.Map;
import controllers.Point;


/**
 *
 * @author Jens
 */

public class CreateNewGame extends javax.swing.JFrame{

	private String scenario;
    /**
     * Creates new form CreateNewGame
     */
	
	private WindowManager windowManager;
	
    public CreateNewGame() {
        initComponents();
    }

    public CreateNewGame(WindowManager windowManager) {
    	this.windowManager = windowManager;
        initComponents();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        btn_createGame = new javax.swing.JButton();
        btn_createGame.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		
        		FileReader fr;
        		double X1 = -1;
        		double Y1 = -1;
        		double X2 = -1;
        		double Y2 = -1;
        		double X3 = -1;
        		double Y3 = -1;
        		double X4 = -1;
        		double Y4 = -1;
        		double X5 = -1;
        		double Y5 = -1;
				try {
					String gameName = txt_gameName.getText().toLowerCase();
					fr = new FileReader("C:\\logs\\" + gameName + ".txt");
	        		BufferedReader br = new BufferedReader(fr); 
	        		String s; 
	        		try {
	        			for (int i = 0; i < 11; i++) {
	        				if (i == 1) {
	        				s = br.readLine();
	        				X1 = Double.parseDouble(s);
	        				}
	        				if (i == 2) {
	        				s = br.readLine();
	        				Y1 = Double.parseDouble(s);
	        				}
	        				if (i == 3) {
	        				s = br.readLine();
	        				X2 = Double.parseDouble(s);
	        				}
	        				if (i == 4) {
	        				s = br.readLine();
	        				Y2 = Double.parseDouble(s);
	        				}
	        				if (i == 5) {
	        				s = br.readLine();
	        				X3 = Double.parseDouble(s);
	        				}
	        				if (i == 6) {
	        				s = br.readLine();
	        				Y3 = Double.parseDouble(s);
	        				}
	        				if (i == 7) {
	        				s = br.readLine();
	        				X4 = Double.parseDouble(s);
	        				}
	        				if (i == 8) {
	        				s = br.readLine();
	        				Y4 = Double.parseDouble(s);
	        				}
	        				if (i == 9) {
	        				s = br.readLine();
	        				X5 = Double.parseDouble(s);
	        				}
	        				if (i == 10) {
	        				s = br.readLine();
	        				Y5 = Double.parseDouble(s);
	        				}
	        			}
	        			System.out.println(X1);
	        			System.out.println(Y1);
	        			System.out.println(X2);
	        			System.out.println(Y2);
	        			System.out.println(X3);
	        			System.out.println(Y3);
	        			System.out.println(X4);
	        			System.out.println(Y4);
	        			System.out.println(X5);
	        			System.out.println(Y5);
	        			

					} catch (IOException e1) {
						e1.printStackTrace();
					} 
		        		try {
							fr.close();
						} catch (IOException e1) {
							e1.printStackTrace();
						} 
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
        		  
        		  Transport NeutralTransporter = new Transport(new Point (X1,Y1));
        		  	Map.getMap().addDrawAbleObject(null);
      				Map.getMap().addDrawAbleObject(NeutralTransporter);
      				//System.out.print(Map.getMap().dObjects.get(0));
      				Map.getMap().dObjects.get(1).addWaypoint(Map.getMap().dObjects.get(1), new Point(X2, Y2));
      				Map.getMap().dObjects.get(1).addWaypoint(Map.getMap().dObjects.get(1), new Point(X3, Y3));
      				Map.getMap().dObjects.get(1).addWaypoint(Map.getMap().dObjects.get(1), new Point(X4, Y4));
      				Map.getMap().dObjects.get(1).addWaypoint(Map.getMap().dObjects.get(1), new Point(X5, Y5));
      				
      				
        	}
        });
        btn_createGame.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		windowManager.showWindow(windowManager.gameLobby);
        	}
        });
        jButton2 = new javax.swing.JButton();
        jButton2.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		windowManager.showWindow(windowManager.mainMenu);
        	}
        });
        jLabel5 = new javax.swing.JLabel();
        txt_gameName = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CMS - Create Game");

        jLabel1.setText("Number of teams:");

        jLabel2.setText("Number of players per team:");

        jLabel3.setText("Maximum play time (minutes):");

        jLabel4.setText("2");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3", "4" }));

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60" }));

        btn_createGame.setText("Create Game");

        jButton2.setText("Cancel");

        jLabel5.setText("Game name:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_createGame)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txt_gameName)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txt_gameName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_createGame)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    
    public String getScenario(){
		return this.scenario;
	}
    
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CreateNewGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CreateNewGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CreateNewGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CreateNewGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CreateNewGame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_createGame;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txt_gameName;
    // End of variables declaration//GEN-END:variables
}
